package com.android.ailaoshan.task.text;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.describtion.TextOperate;


public class TextFragment extends Fragment {

    private Context mContext;

    private MyHandler myHandler = null;

    private View fragmentView;
    private EditText inputBox;
    private TextView saveText;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();

        Log.d("fz","onCreateView被调用");

        fragmentView = inflater.inflate(R.layout.text_fragment,container,false);
        return fragmentView;
    }

    public void onStart(){
        super.onStart();

        inputBox = fragmentView.findViewById(R.id.collection_text_input);
        saveText = fragmentView.findViewById(R.id.collection_text_save_btn);


        Log.d("fz","onStart被调用");
    }

    public void onResume(){
        super.onResume();

        listenBtnClick();

        Log.d("fz","onResume被调用");
    }

    /**
     * 监听按钮点击
     */
    private void listenBtnClick(){
        saveText.setOnClickListener(
           (v) -> {
               String inputString = inputBox.getText().toString().trim();
               // 检查文本是否为空
               if(inputString.equals("")){
                   Toast.makeText(mContext,"输入文本不能为空",Toast.LENGTH_SHORT).show();
               }else{
                   // 保存按钮隐藏
                   saveText.setVisibility(View.INVISIBLE);
                   // 生成文本文件名
                   String fileName = System.currentTimeMillis() + ".txt";
                   // 存本地，存数据库
                   myHandler = new MyHandler();
                   TextOperate textOperate = new TextOperate(myHandler);
                   textOperate.saveText(inputString,fileName);
               }
           }
        );
    }

    public void onPause(){
        super.onPause();

        Log.d("fz","onPause被调用");
    }

    public void onStop(){
        super.onStop();

        Log.d("fz","onStop被调用");
    }

    public void onDestroyView(){
        super.onDestroyView();

        Log.d("fz","onDestroyView被调用");
    }

    public void onDestroy(){
        super.onDestroy();

        fragmentView = null;
        inputBox = null;
        saveText = null;
        mContext = null;
        myHandler = null;
        Log.d("fz","onDestroy被调用");
    }

    public void onDetach(){
        super.onDetach();

        Log.d("fz","onDetach被调用");
    }

    /**
     * 执行回调
     */
    public class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg){
            switch (msg.what){
                // 存储成功
                case 310:{
                    if(inputBox != null){
                        inputBox.setText("");
                    }
                    if(saveText != null){
                        saveText.setVisibility(View.VISIBLE);
                    }
                    break;
                }
                // 存储失败
                case 311:{
                    if(saveText != null){
                        saveText.setVisibility(View.VISIBLE);
                    }
                    if(mContext != null){
                        Toast.makeText(mContext,"存储文本失败",Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
            }
        }
    }
}
